import { reqCartList, reqAddCar, reqDeleteCart, reqChangeCheckedById } from "@/api"


const shopcart= {
    state: {
        cartList: []
    },
    mutations: {
        GETCARTLIST(state, data) {
            state.cartList = data
        }
    },
    actions: {
        async getCartList({ commit }) {
            let res = await reqCartList()
            if (res.code === 200) commit('GETCARTLIST', res.data)
        },
        async updateCartList(context, { type, num, cart }) {
            let data = 0
            switch (type) {
                case 'plus':
                    data = 1
                    break;
                case 'mins':
                    if (cart.skuNum > 1) {
                        data = -1
                    }
                    break;
                case 'change':
                    if (!isNaN(num) && parseInt(num) > 0) {
                        data = parseInt(num) - cart.skuNum
                    }
                    break;
            }
            let res = await reqAddCar(cart.skuId, data)
            if (res.code === 200) {
                return 'ok'
            } else {
                throw new Error('no')
            }
        },
        async deleteCart(context, cart) {
            let res = await reqDeleteCart(cart.skuId)
            if (res.code = 200) { return 'ok' } else {
                throw new Error('no')
            }
        },
        deleteAllChecked({ getters, dispatch }) {
            let promiseArr=[]
                getters.cartList.cartInfoList.forEach((cart) => {
                    if (cart.isChecked === 1) {
                        let promise=dispatch('deleteCart', cart)
                        promiseArr.push(promise)
                    }
                })
                return Promise.all(promiseArr)
        },
        async changeCheckedById(context, { skuId, isChecked }) {
            let res = await reqChangeCheckedById(skuId, isChecked.toString())
            if (res.code = 200) {
                return 'ok'
            } else {
                throw new Error('no')
            }
        },
        changeAllChecked({ dispatch, state, getters }, isAllChecked) {
            let promiseArr=[]
                if (isAllChecked) {
                    getters.cartList.cartInfoList.forEach(cart => {
                        if (cart.isChecked === 0){
                            let promise=dispatch('changeCheckedById', { skuId: cart.skuId, isChecked: 1 })
                            promiseArr.push(promise)
                        } 
                    });
                } else {
                    getters.cartList.cartInfoList.forEach(cart => {
                        if (cart.isChecked === 1) {
                            let promise=dispatch('changeCheckedById', { skuId: cart.skuId, isChecked: 0 })
                            promiseArr.push(promise)
                        }
                    });
                }
            return Promise.all(promiseArr)
        }
    },
    getters: {
        cartList(state) {
            return state.cartList[0] || {}
        }
    }
}
export default shopcart